<?php include_once '/../Controllers/ProductController.php';?>
<?php
if(isset($_GET['request'])){
	$requestJson = json_decode($_GET['request'], true);
	$keyRequest = "";
	$valueOfKeyRequest="";
	
	foreach ($requestJson as $key => $value) {
		$keyRequest = $key;
		$valueOfKeyRequest = $value;
	}
	
	switch (strtolower($keyRequest)){
		case "getcabinetimages";
			$productId = $requestJson['getcabinetimages']['cabinetid'];
			$item = ProductController::getProductImages($productId);
			if($item !=''){
				header('Content-type: application/json');
				echo json_encode(array("".DBConsts::$CABINETIMAGES.""=>$item));
			}
			
			break;
		case "getcabinets":
			$products = "";
			if(isset($requestJson['getcabinets']['categoryid']) && isset($requestJson['getcabinets']['page'])){
				$categoryId = $requestJson['getcabinets']['categoryid'];
				$page = $requestJson['getcabinets']['page'];
				$products = ProductController::getProductsByCategoryLimit($categoryId, $page, DBConsts::$COUNT);
			}
			else if(isset($requestJson['getcabinets']['categoryid'])){
				$categoryId = $requestJson['getcabinets']['categoryid'];
				$products = ProductController::getProductsByCategoryID($categoryId);
			}
			else if(isset($requestJson['getcabinets']['page'])){
				$page = $requestJson['getcabinets']['page'];
				$products = ProductController::getProducts($page, DBConsts::$COUNT);
			}
			else if(isset($requestJson['getcabinets']['code'])){
				$productId = $requestJson['getcabinets']['code'];
				$product = ProductController::getProductsByID($productId);
				if($product !=''){
					header('Content-type: application/json');
					echo json_encode(array("".DBConsts::$CABINETS.""=>$product));
				}
				break;
			} else if(isset($requestJson['getcabinets']['isstar'])){
				$isStar = $requestJson['getcabinets']['isstar'];
				if($isStar == "true"){
					$isStar = 1;
				} else if($isStar == "false"){
					$isStar = 0;
				} else {
					echo json_encode(array("null"=>"null"));
					break;
				}
				$product = ProductController::getAllProductsByIsStar($isStar);
				if($product !=''){
					header('Content-type: application/json');
					echo json_encode(array("".DBConsts::$CABINETS.""=>$product));
				}
				break;
			} else if(isset($requestJson['getcabinets']['soldnumber'])){
				$numberTopSold = $requestJson['getcabinets']['soldnumber'];
				if($numberTopSold > 0){
					$product = ProductController::getAllProductsByHighestNumberSold($numberTopSold);
					if($product !=''){
						header('Content-type: application/json');
						echo json_encode(array("".DBConsts::$CABINETS.""=>$product));
						return;
					}
				}
				echo json_encode(array("null"=>"null"));
				break;
			}
			else if(isset($requestJson['getcabinets'])){
				$product = ProductController::getAllProducts();
				if($product !=''){
					header('Content-type: application/json');
					echo json_encode(array("".DBConsts::$CABINETS.""=>$product));
				}
				break;
			}
			if($products !=''){
				header('Content-type: application/json');
				echo json_encode(array("".DBConsts::$CABINETS.""=>$products));
			}
			else {
				echo "Your API is incorrect";
			}
			break;
		case "getcabinetcategories":
			$productcategories = ProductController::getProductCategories();
			if($productcategories !=''){
				header('Content-type: application/json');
				echo json_encode(array("".DBConsts::$CABINETCATEGORIES.""=>$productcategories));
			}
			break;
		case "getbanners":
			$banners = ProductController::getAllBanners();
			if($banners !=''){
				header('Content-type: application/json');
				echo json_encode(array("".DBConsts::$BANNERS.""=>$banners));
			}
			break;
		case "getslides":
			$slides = ProductController::getAllSlides();
			if($slides !=''){
				header('Content-type: application/json');
				echo json_encode(array("".DBConsts::$SLIDES.""=>$slides));
			}
			break;
			
		case "getmaterials":
			$materials = ProductController::getAllMaterials();
			if($materials !=''){
				header('Content-type: application/json');
				echo json_encode(array("".DBConsts::$MATERIALS.""=>$materials));
			}
			break;
		default:
			header('Content-type: application/json');
			echo json_encode(array("null"=>"null"));
			break;
	}

}	
?>